package com.zto.sch.base.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2020-12-24 15:40
 **/
public class DateJsonSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        String dateStr = "";
        if (Objects.nonNull(date)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            simpleDateFormat.format(date);
            dateStr = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
        }

        jsonGenerator.writeString(dateStr);

    }
}
